package com.qmyx.supply.admin.controller;

import com.qmyx.supply.admin.annotation.oplog.annotation.OperationLog;
import com.qmyx.supply.admin.model.request.LoginRequest;
import com.qmyx.supply.admin.model.request.ModifyPwdRequest;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.api.ResultCode;
import com.ytjj.qmyx.supplychain.common.constants.SysLogConstants;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import com.ytjj.qmyx.supplychain.mapper.entity.YxOrderExceptionNote;
import com.ytjj.qmyx.supplychain.service.BankerService;
import com.ytjj.qmyx.supplychain.service.OrderExceptionNoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Description 店家登陆
 * @ClassName BankerLoginController
 * 
 * 
 * @Version 1.0
 **/

@Slf4j
@RestController
@RequestMapping("/banker")
public class BankerLoginController {
    @Resource
    private OrderExceptionNoteService orderExceptionNoteService;
    @Resource
    private BankerService bankerService;

    /**
     * @Description 店家登陆
     * @Autho swordsmanye
     */
    @PostMapping("/login")
    @OperationLog(optModule = "登录模块", optType = SysLogConstants.LOGIN_OPERATION, desc = "后台登录")
    public CommonResult login(@RequestBody LoginRequest loginRequest) throws UnsupportedEncodingException {
//        log.info("===tel:{},pwd:{},verCode:{},verKey:{}", loginRequest.getTel(), loginRequest.getPwd(), loginRequest.getVerCode(), loginRequest.getVerKey());
        return bankerService.bankerLogin(loginRequest.getVerCode(), loginRequest.getVerKey(), loginRequest.getTel(), loginRequest.getPwd());
    }

    /**
     * 获取店家信息
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    public CommonResult getUserInfo(HttpServletRequest request) {
        return CommonResult.success(bankerService.getUserInfo(request.getHeader("token")));
    }


    /**
     * @Description 修改账号密码
     */
    @PostMapping("/modifiedPassword")
    public CommonResult modifiedPassword(@RequestBody ModifyPwdRequest request) {
        YxBanker userInfo = bankerService.getUserInfo();
        request.setTel(userInfo.getTel());
        bankerService.modifiedPassword(request.getTel(), request.getPwd(), request.getNewPwd());
        return CommonResult.success(ResultCode.SUCCESS);
    }

    @PostMapping("/insertNote")
    public CommonResult insertNote(@RequestBody YxOrderExceptionNote yxOrderExceptionNote){
        String name = bankerService.getUserInfo().getName();
        yxOrderExceptionNote.setAdminName(name);
        if(StringUtils.isEmpty(yxOrderExceptionNote.getContent())){
            throw new ApiException("内容不能为空！");
        }
        yxOrderExceptionNote.setContent("【备注】"+yxOrderExceptionNote);
        boolean result = orderExceptionNoteService.insertByBean(yxOrderExceptionNote);
        if (result){
            return CommonResult.success(1);
        }
        return CommonResult.failed();
    }

    /**
     * 异常订单备注
     * @param id 异常订单表主键id
     * @return CommonResult
     */
    @GetMapping("/selectNote")
    public CommonResult selectNote (@RequestParam Integer id){
        List<YxOrderExceptionNote> list = orderExceptionNoteService.selectByOrderExceptionId(id);
        return CommonResult.success(list);
    }
}
